package com.gblfy.httpclinet.service.impl;

import com.gblfy.httpclinet.consts.HttpConst;
import com.gblfy.httpclinet.dto.HttpContainer;
import com.gblfy.httpclinet.service.HttpClientService;
import com.gblfy.httpclinet.util.HttpClientUtil;
import com.gblfy.httpclinet.util.SpringContextUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * http转http接口处理处理
 *
 * @author gblfy
 * @date 2021-10-12
 */
@Service
public class HttpClientServiceImpl implements HttpClientService {

    /**
     * 处理HttpClientget请求（无参）
     *
     * @param paramRequest 请求
     * @return 返回报文
     * @throws Exception
     */
    @Override
    public String receiveHttpClientGetToHttp(HttpServletRequest paramRequest) throws Exception {

        //获取请求的url标识
        String requestURI = paramRequest.getRequestURI();
        StringBuffer requestURL = paramRequest.getRequestURL();
        // String tServicename = paramRequest.getServletPath();// 从请求中获得服务名

        System.out.println("requestURI:" + requestURI);
        System.out.println("requestURL:" + requestURL);
        // System.out.println("tServicename:" + tServicename);

        //根据url标识区分获取第三方/内部系统指定接口的信息
        // 声明局部变量
        HttpContainer publicHttpClient = (HttpContainer) SpringContextUtils.getBeanById(requestURI);
        String url = publicHttpClient.getUrl();
        System.out.println("获取到的url:" + url);


        // ----------------------------业务特殊处理逻辑 ----------------------------
        String token = "";
        String batch_no = "";
        String resXml = "";

        //需求案例：根据URL拼接参数请求第三方携带token和batch_no参数
        if (HttpConst.GET_NOPARAMS.equals(requestURI)) {
            token = paramRequest.getParameter("token");
            batch_no = paramRequest.getParameter("batch_no");
            System.out.println("接收到的鉴权token: " + token + " ,接收到的批处理编码: " + batch_no);

            //这里需要容错处理
            resXml = HttpClientUtil.doGet(url + "?token=" + token + "&batch_no=" + batch_no);
        }
        return resXml;
    }

    /**
     * 处理HttpClientget请求（json入参）
     *
     * @param paramRequest  请求
     * @param requestBody 请求主体
     * @return 返回报文
     * @throws Exception
     */
    @Override
    public String receiveHttpClientPostToHttp(HttpServletRequest paramRequest, String requestBody) throws Exception {
        //获取请求的url标识
        String requestURI = paramRequest.getRequestURI();
        StringBuffer requestURL = paramRequest.getRequestURL();

        System.out.println("requestURI:" + requestURI);
        System.out.println("requestURL:" + requestURL);

        //根据url标识区分获取第三方/内部系统指定接口的信息
        // 声明局部变量
        HttpContainer publicHttpClient = (HttpContainer) SpringContextUtils.getBeanById(requestURI);
        String url = publicHttpClient.getUrl();
        System.out.println("获取到的url:" + url);


        // ----------------------------业务特殊处理逻辑 ----------------------------
        String resXml = "";

        //需求案例：根据URL拼接参数请求第三方携带token和batch_no参数
        if (HttpConst.POST_PARAMS.equals(requestURI)) {
            //这里需要容错处理
            resXml = HttpClientUtil.postJosnContent(url, requestBody);
        }
        return resXml;
    }
}
